<?php


namespace app\admin\controller\shop;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Shop extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $where[] = ['s.is_del', '=', 0];
            $where[] = ['s.status', 'in', [1,2]];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['s.id', '=', $filter['id']];
                }
                if (isset($filter['operation_status'])) {
                    $where[] = ['s.operation_status', '=', $filter['operation_status']];
                }
                if (isset($filter['shop_name'])) {
                    $where[] = ['s.shop_name', 'like', '%' . $filter['shop_name'] . '%'];
                }
                if (isset($filter['people'])) {
                    $where[] = ['s.people', 'like', '%' . $filter['people'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $where[] = ['s.phone', 'like', '%' . $filter['phone'] . '%'];
                }

                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['s.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['s.city', '=', $city['name']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['s.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //0待审核 1审核通过 2账户关闭  3审核失败
            $_list = Db::name('shop')->alias('s')
                ->leftJoin('member m','s.member_id=m.id')
                ->field('s.*,m.username')
                ->page($page, $limit)
                ->where($where)
                ->order('s.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //0待审核 1审核通过 2账户关闭  3审核失败
            $total =  Db::name('shop')->alias('s')
                ->leftJoin('member m','s.member_id=m.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign([
            'proviec'=>$proviec
        ]);

        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed|void
     * todo 图标url map_logo_url
     */
    public function edit()
    {
        $business_id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $business_info = Db::name('shop')
                ->where('is_del', '=', 0)
                ->where('id', $data['id'])
                ->update($data['row']);
            $this->success('操作成功');
        }
        $data = Db::name('shop')
            ->where('id', $business_id)
            ->find();
        $salesperson=Db::name('salesperson')->find($data['salesperson_id']);
        $data['salesperson'] =$salesperson['id'].'----'.$salesperson['name'];

        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });
        $this->assign([
            'data' => $data,
            'proviec' => $proviec_list,
            'city_list' => $city_list,
            'area_list' => $area_list,
        ]);
        return $this->fetch();
    }

    //重置密码
    public function editps()
    {
        $business_id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $password=encrypt_password(trim($data['password']),$data['salt']);
            Db::name('member')->update([
                'id'=>$data['member_id'],
                'password'=>$password
            ]);
            $this->success('操作成功');
        }

        $shop_info = Db::name('shop')
            ->where('id', $business_id)
            ->find();
        $member_info=Db::name('member')->where('id',$shop_info['member_id'])->find();

        $this->assign([
            'shop_info' => $shop_info,
            'member_info' => $member_info,
        ]);
        return $this->fetch();
    }

    public function editStatus()
    {
        $business_id = $this->request->param('id/d', 0);
        $data = Db::name('shop')->find($business_id);
        $status = $data['status'] == 2 ? 1 : 2;
        $update = array(
            'id' => $business_id,
            'status' => $status,
        );
        $result = Db::name('shop')->update($update);
        if ($result) {
            return ['code' => 200, 'data' => '', 'msg' => '切换成功'];
        } else {
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function getc_cate2()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function funtlist()
    {
        $id = $this->request->param('sale_id/d', 0);

        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $where[] = ['type', 'in', [2,4,6]];
            $where[] = ['shop_id', '=', $id];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
            }

            $_list = Db::name('member_finance')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['finish_time']) {
                    $_list[$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                }
            }
            //获取数量
            $total = Db::name('member_finance')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'sale_id' => $id,
        ]);
        return $this->fetch();
    }

}